package com.hqhsc.module.manage.activityuser.service;

import com.hqhsc.module.common.dao.CommonDao;
import com.hqhsc.module.common.domain.OffLineChannelElement;
import com.hqhsc.module.common.service.FileService;
import com.hqhsc.module.common.service.ManageMemcacdedClient;
import com.hqhsc.module.log.service.LogService;
import com.hqhsc.module.login.domain.User;
import com.hqhsc.module.manage.activity.dao.ActivityManageDao;
import com.hqhsc.module.manage.activity.domain.Activity;
import com.hqhsc.module.manage.activityuser.dao.ActivityUserManageDao;
import com.hqhsc.module.manage.activityuser.domain.ActivityUser;
import com.hqhsc.module.manage.channel.dao.ChannelManageDao;
import com.hqhsc.module.manage.channel.domain.Channel;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 *
 * @author jiangyin
 */
@Service
public class ActivityUserManageService {

	static Logger logger = LoggerFactory.getLogger(ActivityUserManageService.class);

	@Autowired
	private ManageMemcacdedClient memcachedClient;
	@Autowired
	private ActivityUserManageDao activityUserManageDao;
    @Autowired
    private ChannelManageDao channelManageDao;
    @Autowired
    private FileService fileService;
    @Autowired
    private ActivityManageDao activityManageDao;
    @Autowired
    private CommonDao commonDao;
    @Autowired
    private Properties appProps;
    @Autowired
    private LogService logService;

    public Map<String , Object> getActivityUserList(int is_import , String is_buy , long activity_id , long channel_id , String user_name , String orderby_request , int start , int size) {

        String orderby = null;
        String orderby_type = null;
        if(StringUtils.isNotEmpty(orderby_request)) {
            orderby = orderby_request.split(",")[0];
            orderby_type = orderby_request.split(",")[1];
        }

        long total = this.activityUserManageDao.getActivityUserCount(is_import , is_buy , activity_id , channel_id , user_name);
        List<ActivityUser> activity_user_list = this.activityUserManageDao.getActivityUserList(is_import , is_buy , activity_id , channel_id , user_name , orderby , orderby_type , start , size);

        Map<String , Object> map = new HashMap<String, Object>();
        map.put("total" , total);
        map.put("activity_user_list" , activity_user_list);
        return map;
    }

    public List<ActivityUser> getActivitySignUpUserList(int is_import , long activity_id) {
        return this.activityUserManageDao.getActivityUserList(is_import , null , activity_id , -1 , null , null , null , 0 , 10000);
    }


    public int doImportActivityUser(HttpServletRequest request , CommonsMultipartFile file_upload , User user) {
        try{
            if(!file_upload.isEmpty()) {
                //文件上传
                String base_path = request.getSession().getServletContext().getRealPath("/");
                String file_path= appProps.getProperty("activity.user.import.dbpath");
                String save_path = base_path + file_path;
                String file_sys_name = UUID.randomUUID().toString();
                Map<String , String> map = this.fileService.uploadFile(file_upload , save_path , file_sys_name);

                //文件入库
                this.commonDao.fileImport(map.get("origin_name") , file_sys_name , file_path , user.getUser_id());

                //文件解析
                List<OffLineChannelElement> list = this.fileService.analysisExcel(map.get("file_path"));

                //批量插入导入的用户
                this.activityUserManageDao.batchImportActivityUser(list , user.getUser_id());

                this.logService.insertLog(user.getUser_id() , "导入用户"+file_sys_name , "");

                return 1;
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return 0;
    }

    public List<Channel> getChannelList() {
        return this.channelManageDao.getChannelList(0 , null , -1 , null , null , 0 , 1000);
    }

    public List<Activity> getActivityList() {
        return this.activityManageDao.getActivityList(0, null, -1, null, null, 0, 1000);
    }

}
